package ipgather.core.ip;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.Proxy.Type;

import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;

import java.net.URL;
import java.net.URLConnection;

public class IpTest {

	public static void main(String[] args) {
		long time = System.currentTimeMillis();
		InetSocketAddress addr = null;
		addr = new InetSocketAddress("202.101.242.62", 80);
		Proxy proxy = new Proxy(Type.HTTP, addr);
		try {
			URL url = new URL("http://yunpan.360.cn");
			URLConnection connection = url.openConnection(proxy);
			connection.setConnectTimeout(2000);
			if(0!=connection.getDate()&&convertStreamToString(connection.getInputStream()).startsWith("<!DOCTYPE html><html  ><head><meta charset=\"utf-8\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><meta name=\"renderer\" content=\"webkit\"><link rel=\"icon\" href=\"/favicon-16.ico\" sizes=\"16x16\"><link rel=\"icon\" href=\"/favicon-32.ico\" sizes=\"32x32\"><meta name=\"Keywords\" content=\"360云盘,免费网盘,网络硬盘,最安全网盘,最大免费网盘,最快的网络硬盘,最好的网盘,无限空间,同步盘,共享群,文件共享群,外链分享,离线下载,云存储\">")){
				System.out.println("成功");
			}else{
				System.out.println("失败");
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println(System.currentTimeMillis() - time);
	}

	public static String convertStreamToString(InputStream is) {
		if (null == is){
			return "";
		}
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line.trim());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();

	}
}
